﻿/**
 * @file		ct_dd_pmc_ctc.c
 * @brief		PMC CTC driver Component Test
 * @note		None
 * @attention	None
 * 
 * <B><I>Copyright 2016 Socionext Inc.</I></B>
 */

#include <stdlib.h>
#include <string.h>
#include "driver_common.h"
#include "dd_pmc_ctc.h"
#include "pmc.h"

/*----------------------------------------------------------------------*/
/* Definition															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Enumeration															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Structure															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Global Data															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Local Method Definition												*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Local Function														*/
/*----------------------------------------------------------------------*/
static VOID ct_dd_pmc_ctc_callback(VOID);

/**
CTC callback function.
*/
static VOID ct_dd_pmc_ctc_callback(VOID)
{
	Ddim_Print(("CTC Callback\n"));
}

/*----------------------------------------------------------------------*/
/* Global Function														*/
/*----------------------------------------------------------------------*/
/**
 * @brief  Command main function for CTC test.
 * @param  int argc	:The number of parameters
 *		   char** argv	:The value of parameters
 * @return VOID
 *
 */
VOID Ct_Dd_Pmc_Ctc_Main(int argc, char** argv)
{
	INT32				ret;
	int					param2;
	int					param3;
	T_DD_PMC_CTC_Ctrl	ctrl;
	UCHAR				status;
	ULONG				counter;
	
	if(strcmp(argv[1], "ctrl") == 0){
		// get 2nd paramter.
		param2 = atoi(argv[2]);
		if (param2 == 0) {
			// set control data.
			ctrl.interrupt_enable	= 0;
			ctrl.select_wait		= 0;
			ctrl.clock_length		= 0;
			// CTC Control.
			ret = Dd_PMC_CTC_Ctrl(&ctrl);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_CTC_Ctrl : OK\n"));
				// register data output.
				Ddim_Print(("--- CTC Register ---\n"));
				Ddim_Print(("INTCTL : 0x%08x\n", IO_PMC.CTC.INTCTL.word));
				Ddim_Print(("OBCLEN : 0x%08x\n", IO_PMC.CTC.OBCLEN.word));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_CTC_Ctrl : NG  ret=0x%08x\n", ret));
			}
		}
		else if (param2 == 1) {
			// set control data.
			ctrl.interrupt_enable	= 1;
			ctrl.select_wait		= 1;
			ctrl.clock_length		= 0xFFFF;
			// CTC Control.
			ret = Dd_PMC_CTC_Ctrl(&ctrl);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_CTC_Ctrl : OK\n"));
				// register data output.
				Ddim_Print(("--- CTC Register ---\n"));
				Ddim_Print(("INTCTL : 0x%08x\n", IO_PMC.CTC.INTCTL.word));
				Ddim_Print(("OBCLEN : 0x%08x\n", IO_PMC.CTC.OBCLEN.word));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_CTC_Ctrl : NG  ret=0x%08x\n", ret));
			}
		}
		else if (param2 == 2) {
			// input paramter error.
			ret = Dd_PMC_CTC_Ctrl(NULL);
			Ddim_Print(("Dd_PMC_CTC_Ctrl : NG  ret=0x%08x\n", ret));
		}
		else {
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else if(strcmp(argv[1], "start") == 0){
		// get 2nd paramter.
		param2 = atoi(argv[2]);
		if (param2 == 0) {
			ret = Dd_PMC_CTC_Start((VP_CALLBACK)ct_dd_pmc_ctc_callback);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_CTC_Start : OK\n"));
				// register data output.
				Ddim_Print(("--- CTC Register ---\n"));
				Ddim_Print(("CTL     : 0x%08x\n", IO_PMC.CTC.CTL.word));
				Ddim_Print(("INTSTAT : 0x%08x\n", IO_PMC.CTC.INTSTAT.word));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_CTC_Start : NG  ret=0x%08x\n", ret));
			}
		}
		else {
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else if(strcmp(argv[1], "get") == 0){
		if(strcmp(argv[2], "status") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			if (param3 == 0) {
				// data clear.
				status = 0;
				// Get Measuring end status.
				ret = Dd_PMC_CTC_Get_Measuring_End_Status(&status);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_CTC_Get_Measuring_End_Status : OK\n"));
					// register data output.
					Ddim_Print(("--- CTC Register ---\n"));
					Ddim_Print(("INTSTAT : 0x%08x\n", IO_PMC.CTC.INTSTAT.word));
					// get data output.
					Ddim_Print(("--- Get data -------\n"));
					Ddim_Print(("status  : %d\n", status));
					Ddim_Print(("--------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_CTC_Get_Measuring_End_Status : NG  ret=0x%08x\n", ret));
				}
			}
			else if (param3 == 1) {
				// Input paramter error.
				ret = Dd_PMC_CTC_Get_Measuring_End_Status(NULL);
				Ddim_Print(("Dd_PMC_CTC_Get_Measuring_End_Status : NG  ret=0x%08x\n", ret));
			}
			else{
				Ddim_Print(("please check 3rd parameter!!\n"));
			}
		}
		else if(strcmp(argv[2], "counter") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			if (param3 == 0) {
				// data clear.
				counter = 0;
				// Get Counter value.
				ret = Dd_PMC_CTC_Get_Counter_Value(&counter);
				if (ret == D_DDIM_OK) {
					Ddim_Print(("Dd_PMC_CTC_Get_Counter_Value : OK\n"));
					// register data output.
					Ddim_Print(("--- CTC Register ---\n"));
					Ddim_Print(("INTSTAT : 0x%08x\n", IO_PMC.CTC.CNTVAL));
					// get data output.
					Ddim_Print(("--- Get data -------\n"));
					Ddim_Print(("counter : %d\n", counter));
					Ddim_Print(("--------------------\n"));
				}
				else {
					Ddim_Print(("Dd_PMC_CTC_Get_Counter_Value : NG  ret=0x%08x\n", ret));
				}
			}
			else if (param3 == 1) {
				// Input paramter error.
				ret = Dd_PMC_CTC_Get_Counter_Value(NULL);
				Ddim_Print(("Dd_PMC_CTC_Get_Counter_Value : NG  ret=0x%08x\n", ret));
			}
			else{
				Ddim_Print(("please check 3rd parameter!!\n"));
			}
		}
		else{
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else{
		Ddim_Print(("please check 1st parameter!!\n"));
	}
}
